package com.k.comnote.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.nfc.Tag;
import android.util.Log;

import com.k.comnote.model.Note;
import com.k.comnote.model.NoteTag;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by K on 2015/8/16.
 */
public class NoteDB  {
    public static final String DB_NAME = "note";
    public static final int VERSION = 1;
    private SQLiteDatabase sqLiteDatabase;
    private static NoteDB noteDB;

    private NoteDB(Context context) {
        NoteOpenHelper noteOpenHelper = new NoteOpenHelper(context, DB_NAME, null, VERSION);
        sqLiteDatabase = noteOpenHelper.getWritableDatabase();
    }

    public synchronized static NoteDB getNoteDB(Context context) {
        if(noteDB == null) {
            noteDB = new NoteDB(context);
        }
        return noteDB;
    }

    /**
     * 存储日记内容
     */

    public void saveNote(Note note) {
        Log.d("saveNoteLognoteNull", (note == null) + "");
        if(note != null) {
            ContentValues values = new ContentValues();
            //values.put("id", note.getId());
            values.put("time", note.getTime());
            values.put("content", note.getContent());
            values.put("title", note.getTitle());
            sqLiteDatabase.insert("Notes",null, values);
        }
    }

    /**
     * 读取日记内容
     */

    public List<Note> loadNote() {
        List<Note> list = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query("Notes", null, null, null, null, null, null);
        if(cursor.moveToFirst()) {
            do {
                Note note = new Note();
                note.setTime(cursor.getString(cursor.getColumnIndex("time")));
                note.setContent(cursor.getString(cursor.getColumnIndex("content")));
                note.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                list.add(note);
            } while(cursor.moveToNext());
        }
        return list;
    }

    /**
     * 存储日记标签
     */

    public void saveTag(NoteTag tag) {
        if(tag != null) {
            ContentValues values = new ContentValues();
            values.put("tag", tag.getTag());
            sqLiteDatabase.insert("Tags", null, values);
        }
    }

    /**
     * 读取日记标签
     */

    public List<NoteTag> loadNoteTag() {
        List<NoteTag> list = new ArrayList<>();
        Cursor cursor = sqLiteDatabase.query("Tags" , null, null, null, null, null, null);
        if(cursor.moveToFirst()) {
            do {
                NoteTag tag = new NoteTag();
                tag.setTag(cursor.getString(cursor.getColumnIndex("tag")));
                list.add(tag);
            } while(cursor.moveToNext());
        }

        return list;
    }

    public void deleteNote(Note note) {
        String[] time = {note.getTime()};
        sqLiteDatabase.delete("Notes", "time=?", time);
    }

    public Note queryNote(String time) {
        Cursor cursor = sqLiteDatabase.query("Notes",null, null, null, null, null, null);
        Note note = null;
        if (cursor.moveToFirst()) {
            do {
                if (cursor.getString(cursor.getColumnIndex("time")) == time) {
                    note.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                    note.setTime(time);
                    note.setContent(cursor.getString(cursor.getColumnIndex("content")));
                }
            }while(cursor.moveToNext());
        }

        return note;
    }
}
